package book.manage;


import book.manage.entity.BOOK;
import book.manage.entity.Student;
import book.manage.mapper.BookMapper;
import book.manage.sql.SqlUtil;
import lombok.extern.java.Log;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import java.io.IOException;
import java.util.Scanner;
import java.util.logging.LogManager;


@Log
public class Main {
    public static void main(String[] args) throws IOException {
        try (Scanner scanner = new Scanner(System.in)) {

            LogManager manager = LogManager.getLogManager();// 获取日志管理器
            manager.readConfiguration(Resources.getResourceAsStream("logging.properties"));// 读取配置文件

            while (true) {
                System.out.println("=================================");
                System.out.println("1.录入学生信息");
                System.out.println("2.录入学籍信息");
                System.out.println("3.添加借阅信息");
                System.out.println("4.查看借阅信息");
                System.out.println("5.查看学生信息");
                System.out.println("6.查看书籍信息");
                System.out.print("输入你想要的操作(输入其他数字退出):");
                int input;
                try {
                    input = scanner.nextInt();// 读取整数
                }catch (Exception e){
                    return;
                }
                scanner.nextLine();
                /* 读取换行符,之前使用了 scanner.nextInt() 读取整数,但是这个方法只会读取整数,不会读取换行符,所以我们需要手动读取换行符
                在调用 scanner.nextLine() 之前先调用 scanner.nextLine() 以消耗掉剩余的换行符。
                */
                switch (input) {
                    case 1:
                        addStudent(scanner);
                        break;
                    case 2:
                        addBook(scanner);
                        break;
                    case 3:
                        addBorrow(scanner);
                        break;
                    case 4:
                        showBorrow();
                        break;
                    case 5:
                        showStudent();
                        break;
                    case 6:
                        showBook();
                        break;
                    default:
                        return;
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }


    private static void showStudent(){
        SqlUtil.doSqlWork(mapper->{
            mapper.getStudentList().forEach(student -> {
                System.out.println(student.getSid()+"."+student.getName()+" "+student.getSex()+" "+ student.getGrade()+"级");// 打印学生姓名
            });

        });
    }

    private static void showBook(){
        SqlUtil.doSqlWork(mapper->{
            mapper.getBookList().forEach(book -> {
                System.out.println(book.getBid()+"."+book.getTitle()+"[$ "+book.getPrice()+"]"+"("+ book.getDesc()+")");
            });

        });
    }


    private static void showBorrow(){
        SqlUtil.doSqlWork(mapper->{
            mapper.getBorrowList().forEach(borrow -> {
                System.out.println(borrow.getStudent().getName()+"->"+borrow.getBook().getTitle());// 打印学生姓名和书籍标题
            });

        });
    }

    private static  void  addBorrow(Scanner scanner){
        System.out.print("请输入书籍号:");
        String a = scanner.nextLine();
        int bid = Integer.parseInt(a);// 将字符串转换为整数
        System.out.print("请输入学生号:");
        String b = scanner.nextLine();
        int sid = Integer.parseInt(b);// 将字符串转换为整数
        SqlUtil.doSqlWork(mapper->{mapper.addBorrow(sid,bid);});

    }


    private static void addBook(Scanner scanner){
        System.out.print("请输入书籍标题:");
        String title = scanner.nextLine();
        System.out.print("请输入书籍介绍:");
        String desc = scanner.nextLine();
        System.out.print("请输入书籍价格:");
        String price = scanner.nextLine();
        double p = Double.parseDouble(price);// 将字符串转换为整数
        BOOK book = new BOOK(title,desc,p);// 创建书籍对象
        SqlUtil.doSqlWork(mapper->{
            int i = mapper.addBook(book);// i来接受返回的结果

            if(i>0){
                System.out.println("书籍信息录入成功");

                log.info("新添加一条书籍信息"+ book);// 记录日志
            }else {
                System.out.println("书籍信息录入失败,请重试");
            }
        });

    }



    private static void addStudent(Scanner scanner){
        System.out.print("请输入学生姓名:");
        String name = scanner.nextLine();
        System.out.print("请输入学生性别(男/女):");
        String sex = scanner.nextLine();
        System.out.print("请输入学生年级:");
        String grade = scanner.nextLine();
        int g = Integer.parseInt(grade);// 将字符串转换为整数
        Student student = new Student(name,sex,g);// 创建学生对象
        SqlUtil.doSqlWork(mapper->{
            int i = mapper.addStudent(student);// i来接受返回的结果
            /* 看起来很烦很难对吧，但是我们自己拼接一下就知道了
            doSqlWork方法的参数是一个Consumer<BookMapper>类型的，也就是一个函数，这个函数接受一个BookMapper类型的参数
            Consumer<BookMapper> consumer = mapper->mapper.addStudent(student);
            consumer.accept(bookMapper);consumer函数，参数是mapper,我们传入的是bookMapper,最后就是bookMapper.addStudent(student);
            就是用一个lambda表达式来表示一个函数，这个函数接受一个参数，这个参数是BookMapper类型的，然后调用这个mapper的addStudent方法
            */

            if(i>0){
                System.out.println("学生信息录入成功");

                log.info("插入一条学生信息"+ student);// 记录日志

            }else {
                System.out.println("学生信息录入失败,请重试");
            }
        });

    }
}
